iT邦幫忙

2025 iThome 鐵人賽

DAY 17
0
AI & Data

AI 實戰 30 天:Azure Foundry 與 Vertex AI 全面探索系列 第 17

Day17:AI 模型 API 整合進應用程式

  • 分享至 

  • xImage
  •  

在 Day16 中,我們已經將 AI 模型部署到雲端。接下來的重點,就是如何把這些模型真正整合到應用程式裡,讓使用者能夠透過 前端、行動 App 或後端系統 直接調用模型 API。


🔹 為什麼 API 整合是關鍵?

  • 模型到應用的橋樑:讓 AI 不只是研究成果,而是實際可用的功能。
  • 支援多樣化應用:相同的 API,可以同時支援網頁、App、IoT 裝置。
  • 符合現代架構:REST/gRPC API 已是系統整合的標準模式。
  • 安全與治理:透過 API Gateway、身份驗證與流量控管,能確保 AI 使用可控可管。

🔹 整合流程分解

  1. 模型部署

    • Azure:部署至 Managed Endpoint
    • GCP:部署至 Vertex AI Endpoint
  2. 取得 API Endpoint 與憑證

    • Azure:Endpoint URL + Bearer Token
    • GCP:Endpoint ID + IAM 權限 + API Key / Service Account
  3. 測試呼叫 API

    • 使用 Postman / curl 驗證是否能正確回應
  4. 應用整合

    • 前端 Web:JavaScript/React 直接呼叫
    • 行動 App:Swift/Kotlin 呼叫 REST API
    • 後端服務:C#/Python/Node.js 導入業務流程

🔹 Azure Managed Endpoint vs Vertex AI Endpoints

項目 Azure Managed Endpoint Vertex AI Endpoints
建立方式 Azure ML Studio 一鍵部署 / CLI GCP Console / gcloud / SDK
自動縮放 支援 Auto-scaling,根據需求調整計算資源 同樣支援 Auto-scaling,且可配置 GPU/TPU
存取控管 與 Azure AD 整合,支援 RBAC、Private VNET 與 IAM 整合,支援 API Key / Service Account
整合 SDK Python SDK、REST API、C#、Java Python SDK、REST API、gRPC
適用情境 Microsoft 生態圈、Power Platform 整合 需要 TPU / Google 生態(BigQuery、Cloud Run)

🔹 實作範例

1. 呼叫 Azure Managed Endpoint (Python)

import requests

url = "https://<your-endpoint>.azurewebsites.net/score"
headers = {
    "Authorization": "Bearer <your-token>",
    "Content-Type": "application/json"
}
data = {"input_data": {"text": "Hello Azure AI!"}}

response = requests.post(url, headers=headers, json=data)
print("Prediction:", response.json())

2. 呼叫 Vertex AI Endpoint (Python)

from google.cloud import aiplatform

初始化客戶端

from google.cloud import aiplatform

# 初始化客戶端
aiplatform.init(project="your-project-id", location="us-central1")

endpoint = aiplatform.Endpoint("projects/<project-id>/locations/us-central1/endpoints/<endpoint-id>")

response = endpoint.predict(instances=[{"text": "Hello Vertex AI!"}])
print("Prediction:", response.predictions)

3. 前端 Web 整合範例 (JavaScript / Fetch)

async function callAI() {
  const response = await fetch("https://your-endpoint.com/score", {
    method: "POST",
    headers: {
      "Authorization": "Bearer YOUR_TOKEN",
      "Content-Type": "application/json"
    },
    body: JSON.stringify({ input_data: { text: "Hello from frontend!" } })
  });

  const result = await response.json();
  console.log(result);
}

callAI();

架構圖(文字描述)

典型的整合架構會是這樣:

[ Web 前端 / Mobile App / Backend ] 
          ↓ (HTTPS)
[ API Gateway / Endpoint 認證 ]
          ↓
[ Azure Managed Endpoint / Vertex AI Endpoint ]
          ↓
[ 模型推論 (CPU/GPU/TPU) ]
          ↓
[ 回傳結果至應用程式 ]

小結

Day17 我們正式走完 「模型部署 → API 化 → 應用整合」 這條路。

Azure Managed Endpoint 適合快速整合 Microsoft 生態(Power Apps、Teams、.NET 系統)。

Vertex AI Endpoints 適合需要高彈性、高效能(特別是 TPU、大規模推論)的場景。

👉 下一步(Day18),我們將不只停留在 API 呼叫,而是深入探討 Prompt Engineering 與最佳化技巧,讓模型輸出的結果更貼近需求。


上一篇
Day 16:AI 開發流程 — 從資料到部署的完整旅程
下一篇
Day18:Prompt Engineering 與最佳化技巧
系列文
AI 實戰 30 天:Azure Foundry 與 Vertex AI 全面探索20
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言